package com.smc.common.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import lombok.extern.slf4j.Slf4j;

/**
 * json工具类
 * @author zouqinghua
 * @date 2019年3月30日  下午10:05:40
 *
 */
@Slf4j
public class JsonUtils {

	public static ObjectMapper objectMapper = new ObjectMapper();

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }
    
    public static String toJSONString(Object object) {
        String str = null;
        try {
            str = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
        	log.error("json 处理失败",e);
        }
        return str;
    }
    
    /**
     * 对象转换为json字符串
     * 
     * 配置不写value为null的key  
     * @param object
     * @return
     */
    public static String toJSONStringFilterNull(Object object) {
        String str = null;
        try {
        	//配置不写value为null的key  
        	objectMapper.setSerializationInclusion(Include.NON_NULL);
            str = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
        	log.error("json 处理失败",e);
        }
        return str;
    }
    
    public static <T> T toBean(String json, Class<T> clazz) {
        T t = null;
        try {
            t = objectMapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            log.error("json 处理失败 JsonParseException",e);
        } catch (JsonMappingException e) {
            log.error("json 处理失败 JsonMappingException",e);
        } catch (IOException e) {
            log.error("json 处理失败 IOException",e);
        }
        return t;
    }
    
    public static <K, V> HashMap<K, V> toMap(String json, Class<K> k, Class<V> v) {
        try {
            return objectMapper.readValue(json, getCollectionType(HashMap.class, k, v));
        } catch (Exception e) {
            throw new RuntimeException("json to object error!", e);
        }
    }
    
    public static <K, V> HashMap<K, V> toLinkedHashMap(String json, Class<K> k, Class<V> v) {
        try {
            return objectMapper.readValue(json, getCollectionType(LinkedHashMap.class, k, v));
        } catch (Exception e) {
            throw new RuntimeException("json to object error!", e);
        }
    }

    public static <T> List<T> toList(String json, Class<T> t) {
        try {
            return objectMapper.readValue(json, getCollectionType(ArrayList.class, t));
        } catch (Exception e) {
            throw new RuntimeException("json to object error!", e);
        }
    }
    
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
    	
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }
    
}
